<?php
/**
 *    This file is part of OXID eShop Community Edition.
 *
 *    OXID eShop Community Edition is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    OXID eShop Community Edition is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with OXID eShop Community Edition.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @link      http://www.oxid-esales.com
 * @package   admin
 * @copyright (C) OXID eSales AG 2003-2010
 * @version OXID eShop CE
 * @version   SVN: $Id: list_review.php 27134 2010-04-09 13:50:28Z arvydas $
 */

/**
 * user list "view" class.
 * @package admin
 */
class myinventory extends Article_List
{
    /**
     * Executes parent method parent::render(), passes data to Smarty engine
     * and returns name of template file "list_review.tpl".
     *
     * @return string
     */
    public function render()
    {
        oxAdminList::render();
        $oSmarty = oxUtilsView::getInstance()->getSmarty();
        $oSmarty->assign( "oViewConf", $this->_aViewData["oViewConf"]);
        $oSmarty->assign( "shop", $this->_aViewData["shop"]);

        /*$this->_aViewData["viewListSize"]  = $this->_getViewListSize();
        $this->_aViewData["whereparam"]    = $this->_aViewData["whereparam"] . '&amp;viewListSize='.$this->_getViewListSize();
        $this->_aViewData["menustructure"] = $this->getNavigation()->getDomXml()->documentElement->childNodes;
        $this->_aViewData["articleListTable"] = getViewName('oxarticles');*/
        
        $sWhere = "";
        if ( is_array( $aWhere = oxConfig::getParameter( 'where' ) ) ) {
            /*echo "<pre>";
            print_r($aWhere);
            echo "<br>";*/
            $aKeys = array_keys($aWhere);
            /*print_r($aKeys);
            echo ":->".count($aKeys);*/
            /*---for($i=0; $i<count($aKeys); $i++) {
                //echo $aWhere[$aKeys[$i]]."-<br>";
                if ($aWhere[$aKeys[$i]] != '')
                    $sWhere .= "AND " . $aKeys[$i] . " like '%" . $aWhere[$aKeys[$i]] . "%' ";
            }---*/
            if ($aWhere['oxartnum'] != '')
                $sWhere .= "AND a.oxartnum LIKE '%".$aWhere['oxartnum']."%' ";
            if ($aWhere['oxtitle'] != '')
                $sWhere .= "AND IF(a.oxparentid = '', a.oxtitle, (SELECT b.oxtitle FROM oxarticles b where b.oxid = a.oxparentid)) LIKE '%".$aWhere['oxtitle']."%' ";
            if ($aWhere['oxvarselect'] != '')
                $sWhere .= "AND IF(a.oxvarselect = '', '-', a.oxvarselect) LIKE '%".$aWhere['oxvarselect']."%' ";
            if ($aWhere['invsite'] != '')
                $sWhere .= "AND s.invsite LIKE '%".$aWhere['invsite']."%' ";
            if ($aWhere['invstore'] != '')
                $sWhere .= "AND s.invstore LIKE '%".$aWhere['invstore']."%' ";
            //echo "</pre>";
        }
        //echo $sWhere;
        
        $sortCol = oxConfig::getParameter( 'sortcol' );
        if (empty($sortCol))
            $sortCol = 'oxartnum';
        
        if ($sortCol == 'oxartnum')
            $sSort = "a.oxartnum";
        if ($sortCol == 'oxtitle')
            $sSort = "IF(a.oxparentid = '', a.oxtitle, (SELECT b.oxtitle FROM oxarticles b where b.oxid = a.oxparentid)), IF(a.oxvarselect = '', '-', a.oxvarselect)";
        if ($sortCol == 'oxvarselect')
            $sSort = "IF(a.oxvarselect = '', '-', a.oxvarselect), IF(a.oxparentid = '', a.oxtitle, (SELECT b.oxtitle FROM oxarticles b where b.oxid = a.oxparentid))";
        if ($sortCol == 'invsite')
            $sSort = "s.invsite, s.invstore, s.invrack, s.invlevel";
        if ($sortCol == 'invstore')
            $sSort = "s.invstore, s.invrack, s.invlevel";
        
        $sSql = "SELECT "
                . "a.oxid AS oxid, IF(a.oxparentid = '', a.oxtitle, (SELECT b.oxtitle FROM oxarticles b where b.oxid = a.oxparentid)) AS oxtitle, "
                . "(IF(a.oxbprice=0,(SELECT b.oxbprice FROM oxarticles b where b.oxid = a.oxparentid),a.oxbprice) * invstock) AS invbuysum, "
                . "(a.oxprice * invstock) AS invsellsum, "
                . "IF(oxbprice=0,(SELECT b.oxbprice FROM oxarticles b where b.oxid = a.oxparentid),oxbprice) AS oxbprice, "
                . "a.oxartnum, IF(a.oxvarselect = '', '-', a.oxvarselect) AS oxvarselect, a.oxstockflag, s.invstock, s.invsite, s.invstore, s.invrack, s.invlevel "
                . "FROM oxarticles a, invarticles s "
                . "WHERE a.oxid = s.invoxid "
                    . "AND oxvarcount = 0 "
                    . "AND invstock != 0 "
                    . $sWhere
                . "ORDER BY $sSort ";

        //echo "<pre>".$sortCol.'/'.$sSql."</pre>";
        $aInventory = array();
        $rs = oxDb::getDb(true)->Execute($sSql);
        while (!$rs->EOF) {
            array_push($aInventory, $rs->fields);
            $rs->MoveNext();
        }
        $oSmarty->assign("aInventory", $aInventory);
        $oSmarty->assign("aWhere", $aWhere);
        $oSmarty->assign("sortcol", $sortCol);

        return "myinventory/myinventory.tpl";
    }

    
    
    public function updateInventory($OrderId, $ArticleID, $Amount)
    {
        //echo '*** updateInventory ***';
        $sSql = "SELECT invorderid FROM invshipping WHERE invorderid = '$OrderId' ";
        $rs = oxDb::getDb(true)->Execute($sSql);
        /*echo '<pre>';
        echo $sSql.'<br>';
        echo 'count()='.count($rs->fields).'<br>';
        print_r($rs->fields);
        echo 'EOF: '.$rs->EOF;
        echo '</pre>';*/

        if ($rs->EOF) {
            $sSql = "UPDATE invarticles SET invstock = invstock - $Amount WHERE invoxid = '$ArticleID'";
            /*echo $ArticleID.' - '.$Amount.'<br>';
            echo '<pre>';
            echo $sSql;
            echo '</pre>';*/
            $rs = oxDb::getDb(true)->Execute($sSql);
        }
        
        return;
    }
 
    
    
    public function saveShippingAction($OrderId, $SendDate, $OrderNr)
    {
        $sSql = "INSERT IGNORE INTO invshipping (invorderid, invsenddate, invordernr) "
                . "VALUES ('$OrderId', '$SendDate', '$OrderNr')";
        $rs = oxDb::getDb(true)->Execute($sSql);
        
        return;
    }

}

/*class Order_Inventory extends oxAdminDetails
{
    public function sendorder() {
        $result = parent::sendorder();
        echo '*** It works ***';
        return result;
    }
}*/